package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Entity;
import play.db.jpa.Model;

@Entity
public class TopicRelation extends Model {

	public Long firstTopic;
	public Long secondTopic;
	public String relationName;
	public String description;

	/**
	 * @author AbedelRahman Badawy this method creates a new TopicRelation and
	 *         saves it in the database. It checks whether it has been saved
	 *         before or not.
	 * @param FirstTopic
	 *            long id of the first topic.
	 * @param SecondTopic
	 *            long id of the second topic.
	 * @param RelationName
	 *            String name of the relation
	 * @param description
	 *            String description of the relation
	 * @return String whether relation is saved or not
	 */
	public static String MakeTopicRelation(Long FirstTopic, Long SecondTopic,
			String RelationName, String description) {

		if (TopicRelation
				.find("select tr from TopicRelation tr where tr.firstTopic =? and tr.secondTopic=? and tr.relationName=?",
						FirstTopic, SecondTopic, RelationName).first() == null) {

			TopicRelation Topic_Relation = new TopicRelation();
			Topic_Relation.firstTopic = FirstTopic;
			Topic_Relation.secondTopic = SecondTopic;
			Topic_Relation.relationName = RelationName;
			Topic_Relation.description = description;
			Topic_Relation.save();
			return "Relation is done !";

		} else {

			return "Relation is already done !";

		}

	}

	/**
	 * delete relation between secondTopic topics by searching for the id and
	 * deleting the corresponding relation
	 * 
	 * @author 
	 * @param firstTopic
	 *            first topic in the relation
	 * @param secondTopic
	 *            second topic in the relation
	 * @param relationName
	 *            relation name between the secondTopic topics
	 * @return if the relation is already existing & not deleted before it
	 *         returns 'Deleted!' else 'There is no Relation to Delete!'
	 */
	// public static String DelteRelationsTopics(Long firstTopic,
	// Long secondTopic, String relationName) {
	// if (TopicRelation
	// .find("select tr from TopicRelation tr where tr.firstTopic =? and tr.secondTopic=? and tr.relationName=?",
	// firstTopic, secondTopic, relationName) != null) {
	//
	// TopicRelation relationToBeDeleted = TopicRelation
	// .find("select tr from TopicRelation tr where tr.firstTopic =? and tr.secondTopic=? and tr.relationName=?",
	// firstTopic, secondTopic, relationName).first();
	// relationToBeDeleted.delete();
	// return "Deleted!";
	// } else {
	// return "There is no Relation to Delete!";
	// }
	// }
	//

	public static String DelteRelationsTopics(Long relationId) {
		if (TopicRelation.findById(relationId) != null) {

			TopicRelation relationToBeDeleted = TopicRelation
					.findById(relationId);
			relationToBeDeleted.delete();
			return "Deleted!";
		} else {
			return "There is no Relation to Delete!";
		}
	}

	/**
	 * @author AbedelRahman Badawy this methods returns the name of the relation
	 */
	public String toString() {

		return this.relationName;
	}

	/**
	 * Returns a list of Strings that correspond to the topics' names that are
	 * related to a certain topic
	 * 
	 * @author shereef
	 * 
	 * @param Long
	 *            topicId Id of the topic
	 * 
	 * @return list<String> topics return topics that are related to that topic
	 */
	public static List<String> relatedtopic(Long topicId) {
		List<Long> topicsIds = new ArrayList<Long>();
		List<String> topics = new ArrayList<String>();
		List<TopicRelation> topicsRel = new ArrayList<TopicRelation>();
		if (TopicRelation
				.find("select tr from TopicRelation tr where tr.firstTopic =? or tr.secondTopic=? ",
						topicId) != null) {
			topicsRel = TopicRelation
					.find("select tr from TopicRelation tr where tr.firstTopic =? or tr.secondTopic=? ",
							topicId).fetch();
			for (TopicRelation topicRel : topicsRel) {
				if (topicRel.firstTopic == topicId
						&& !topicsIds.contains(topicRel.secondTopic)) {
					Topic topic = (Topic) IdeaDeveloper
							.find("id=?", topicRel.secondTopic).fetch().get(0);
					topicsIds.add(topicRel.secondTopic);
					topics.add(topic.title);
				}
				if (topicRel.secondTopic == topicId
						&& !topicsIds.contains(topicRel.firstTopic)) {
					Topic topic = (Topic) IdeaDeveloper
							.find("id=?", topicRel.firstTopic).fetch().get(0);
					topicsIds.add(topicRel.firstTopic);
					topics.add(topic.title);
				}
			}
		}

		return topics;
	}
	
	/**
	 * returns the equivalent topic with the given id 
	 * @param topicID
	 * @return Topic
	 * the topic matching the given id
	 */
	 public static Topic topicById(Long topicID) {

	  return Topic.findById(topicID);
	 }
}
